< previous page page_558 next page >

Page 558
9f31265bb40b592b1929e18a47ab28e5.gif
strName = "Bill"

ConvertString(strName)   ' Changes strName to "Microsoft"
Print strName       ' You have added 5 characters to strName
The ConvertString() routine changes the variable strName to the string "Microsoft". By doing so, it overwrites five characters somewhere in memory and possibly corrupts another memory location. This is one of the most common causes of fatal application errors. To avoid this type of thing from happening, you must find out the largest string that could be put into the parameter and declare your string to that size prior to calling the routine. For example, if the ConvertString() routine can return a 100-byte string, you would perform the following:
9f31265bb40b592b1929e18a47ab28e5.gif
strName = Space$(100)    ' Create a 100 byte string
ConvertString(strName)
By preallocating the space, you ensure that C will not overwrite any other memory area except what is used by this string.
The SendMessage API Function
The first example of a Windows API call youll learn about involves the SendMessage() API. This is a very versatile API function, and youll probably use it more than any other function. SendMessage() is used to tell a specific control to perform some operation on itself. You do this by sending a message through Windows to the window youre interested in controlling. The message is received by the control like an event in Visual Basic. The control then responds to this message and performs the appropriate action. Lets look at several examples of using the SendMessage() API.
Finding a String in a List Box
If you have a large amount of items in a list or combo box and you want to find a certain string contained in that list, you would normally have to loop through each item, comparing each item to the string you want to find. This process can take quite a while, especially if the list is very large. Using SendMessage(), you dont have to write a loop; instead, you can have the list box use some built-in routines to perform the searching for you.

 
< previous page page_558 next page >

If you like this book, buy it!